package cn.tedu.DButil;


import cn.tedu.view.StudentView;

import java.io.*;
import java.util.Arrays;
import java.util.Scanner;

public class DBStudent {
    private static final Scanner SCANNER = new Scanner(System.in);
    /*通过序列化将学生信息保存到本地*/
    public static void saveStudent(Object obj, String file) {
        ObjectOutputStream objectOutputStream = null;
        try {
            objectOutputStream = new ObjectOutputStream(new FileOutputStream(new File(file)));
        } catch (IOException e) {
            System.out.println("保存失败，请联系管理员");
        }
        try {
            objectOutputStream.writeObject(obj);
            System.out.println("注册成功，数据已保存");
        } catch (IOException e) {
            System.out.println("系统异常注册失败");
        }
        try {
            objectOutputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (objectOutputStream != null) {
            try {
                objectOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    /*使用反序列化将学生从本地读入*/

    public static void inputStudent(String name) {
        StudentView studentView = new StudentView();

        ObjectInputStream objectInputStream = null;
        try {
            objectInputStream = new ObjectInputStream(new FileInputStream("G:\\Project01\\Student\\" + name + ".txt"));

        } catch (IOException e) {
            System.out.println("查无此人");
            studentView.denglumain("?");
        }

        try {
            Object o = objectInputStream.readObject();


            System.out.println(o);
            System.out.println("学生信息查询成功");
            studentView.denglumain(name);
        } catch (IOException e) {
            System.out.println("学生暂未找到");

        } catch (ClassNotFoundException e) {
            System.out.println("学生暂未找到");

        }
        if (objectInputStream != null) {
            try {
                objectInputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

    /*学生修改基本信息*/
    public static <Student> void changStudent(String name) {


        ObjectInputStream objectInputStream = null;
        try {
            objectInputStream = new ObjectInputStream(new FileInputStream(new File("G:\\Project01\\Student\\" + name + ".txt")));
        } catch (IOException e) {
            System.out.println("查无此人");

        }
        System.out.println("账号密码随便输入并不会执行修改");
        System.out.println("请输入学号");
        String id = SCANNER.next();
        System.out.println("请输入年龄");
        int age = SCANNER.nextInt();
        System.out.println("请输入班级");
        String clazz = SCANNER.next();
        System.out.println("请输入账号");
        String zh = SCANNER.next();
        System.out.println("请输入密码");
        String password = SCANNER.next();
        Student student = new Student(id, name, age, clazz, zh, password);
        DBStudent.saveStudent(student, "G:\\Project01\\Student\\" + name + ".txt");
        System.out.println("完成修改，修改后您的信息为" + student);
        savepassword(name,zh,password);
        System.out.println("账号密码修改成功");
        if (objectInputStream != null) {
            try {
                objectInputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }


    }
    /*试卷本地读取*/

    public static void duqu(String name) {
        Reader reader = null;
        try {
            reader = new BufferedReader(new FileReader("G:\\Project01\\试题\\试题.txt"));
            char b[] = new char[8000];
            int a = 0;
            while ((a = reader.read(b, 0, b.length)) != -1) {
                if (a == b.length) {

                    System.out.print(Arrays.toString(b));
                } else {
                    for (int i = 0; i < a; i++) {
                        System.out.print(b[i]);
                    }
                }
            }
        } catch (IOException e) {
            System.out.println("试题丢失");
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /*获取成绩*/
    public static void getscore(String name) {
        BufferedReader bufferedReader = null;
        ObjectInputStream objectInputStream=null;
        ObjectOutputStream objectOutputStream=null;
        //解决方法
        System.out.println();
        System.out.println("不会的请用空格代替");
        System.out.println();

        System.out.println("请输入答案");
        String next = SCANNER.next();
        Student q=null;
        try {
            bufferedReader = new BufferedReader(new FileReader("G:\\Project01\\答案\\答案.txt"));

            objectInputStream=new ObjectInputStream(new FileInputStream("G:\\Project01\\Student\\" + name + ".txt"));

            Object o = objectInputStream.readObject();
             q=(Student)o;
            String s = next.toUpperCase();
            StringBuffer stringBuffer = new StringBuffer();//存储答案
            int a = 0;
            int score = 0;//用来记录成绩
            //读取成绩
            while ((a=bufferedReader.read()) != -1) {
                stringBuffer.append((char) a);
            }
            char[] chars = s.toCharArray();
            for (int i = 0; i < chars.length; i++) {
                if (chars[i] == stringBuffer.charAt(i)) {
                    score += 5;
                }
            }
           q.setScore(score);

            System.out.println("您的成绩为" + score);

        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        } finally {

            if (objectInputStream!=null){
                try {
                    objectInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (bufferedReader != null) {
                try {
                    bufferedReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        try {
            objectOutputStream=new ObjectOutputStream(new FileOutputStream("G:\\Project01\\Student\\"+name+".txt"));
            objectOutputStream.writeObject(q);
        } catch (IOException e) {
            System.out.println("成绩导出异常");
        }finally {
            if (objectOutputStream!=null){
                try {
                    objectOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }

    /*本方法用来保存账号密码*/
    public static void savepassword(String name, String adm, String password) {
        SaveAP saveAP = new SaveAP(adm, password);
        ObjectOutputStream objectoutputStream = null;
        try {
            objectoutputStream = new ObjectOutputStream(new FileOutputStream("G:\\Project01\\password\\" + name + ".txt"));
            objectoutputStream.writeObject(saveAP);
        } catch (IOException e) {
            System.out.println("密码保存异常");
        } finally {
            if (saveAP != null) {
                try {
                    objectoutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }

    /*登陆验证*/
    public static boolean yanzheng(String name, String adm, String password) {
        ObjectInputStream inputStream = null;
        try {
            inputStream = new ObjectInputStream(new FileInputStream("G:\\Project01\\password\\" + name + ".txt"));
            Object o = inputStream.readObject();
            SaveAP s = (SaveAP) o;
            String a = s.getAdm() + s.getPassword();
            String b = adm + password;
            if (a.equals(b)) {
                System.out.println("Successful");
    return  true;
            }
        } catch (Exception e) {
            System.out.println("用户不存在");
        }finally {
            if (inputStream!=null){
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }
        return false;
    }
}
